<template>
  <el-dialog
    :before-close="handleClose"
    title="设置名称"
    :visible.sync="dialogVisible"
  >
    <el-form
      ref="ruleForm"
      v-loading="loading"
      label-width="100px"
      :model="ruleForm"
      :rules="rules"
      size="mini"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="ruleForm.name"
          placeholder="请输入名称"
          spellcheck="false"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('ruleForm')">保存</el-button>
        <el-button @click="dialogVisible = false">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import agent from "@/api/agent";
import clearWatch from "@/utils/mixin/clearWatch";
export default {
  name: "SetName",
  mixins: [clearWatch],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      ruleForm: {
        name: ""
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }]
      },
      agent_id: ""
    };
  },
  methods: {
    handleClose(done) {
      done();
    },
    show(id) {
      this.dialogVisible = !this.dialogVisible;
      this.agent_id = id;
    },

    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          agent
            .set_name({
              agent_id: this.agent_id,
              name: this.ruleForm.name
            })
            .then(res => {
              this.$message.success(res.data.message);
              this.dialogVisible = false;
              this.$emit("change");
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
